/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class FASTAValidator {

	public void internalStructurValidation(String file) throws Exception{
		System.out.println("internalStructurValidation of:"+ file);
		
		String line = "";
		List<String> list = new ArrayList<String>();
		List<Integer> header = new ArrayList<Integer>();
		int count = 0;
		int seqNo = 0;
		int i = 0;
		try{
			BufferedReader br = new BufferedReader(new FileReader(file));
			System.out.println("br: " +br);
			
			for (line = br.readLine().trim(); line != null; line = br.readLine()){
				i++;
				list.add(line);
				
				if(line.startsWith(">") == true){
					count = count +1;
					header.add(i);
				}
				else{
					if(line.trim().matches("^[ACGTURYKMSWBDHVNacgturykmswbdhvn\\-]*$") == true  | line.trim().matches("[ABCDEFGHIKLMNPQRSTUVWYZXabcdefghiklmnpqrstuvwyzx\\-\\*]*$") == true){	
						if(i == 1){
							if(list.indexOf(line) == 0) {
								seqNo = seqNo +1;
							}
						}
						if(i > 1){
							if(list.get(list.indexOf(line)-1).startsWith(">") == true){
								seqNo = seqNo +1;
							}
						}
					}
					else{
						System.out.println("Wrong entry in line" + i +"! Exception will be thrown!");
						Exception e = new Exception("The uploaded file contains a wrong entry in the line number "+ i +".");
						throw(e);
					}
				}
			}
			}catch(Exception e){
				if(e.getMessage().contains("The uploaded file contains a wrong entry in the sequence line")){
					throw(e);
				}
			}
			
			
			if(header.size() == 0){
				System.out.println("No header in FASTA file! Exception will be thrown!");
				Exception e = new Exception("The uploaded file doesn't contain a sequence header.");
				throw(e);
			}
			if(header.get(0) != 1){
				System.out.println("The first line in FASTA file is no header! Exception will be thrown!");
				Exception e = new Exception("The uploaded FASTA file doesn't start with a sequence header.");
				throw(e);
			}
			if(seqNo == 0){
				System.out.println("The FASTA file doesn't contain any sequence! Exception will be thrown!");
				Exception e = new Exception("The uploaded FASTA file doesn't contain any sequence.");
				throw(e);
			}
			if(header.size() != seqNo){
				if(header.size() > seqNo){
					System.out.println("The FASTA file contains more headers than sequences! Exception will be thrown!");
					Exception e = new Exception("The uploaded FASTA file contains more headers than sequences.");
					throw(e);
				}
				else{ //if(header.size() < seqNo)
					System.out.println("The FASTA file contains more sequences than headers! Exception will be thrown!");
					Exception e = new Exception("The uploaded FASTA file contains more sequences than headers.");
					throw(e);
				}
			}
	

	}
	
}